﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using PomodoroSuite.FrontEnd.Win.Properties;

namespace PomodoroSuite.FrontEnd.Win
{
    public partial class Shell : KryptonForm
    {
        #region " Campos "

        private frmListaTareas listaTareas = null;
        private frmTodoHoy todoHoy = null;
        private bool enDescanso = false;

        #endregion

        #region " Constructor "

        public Shell()
        {
            InitializeComponent();
        }

        #endregion

        #region " Propiedades "

        public bool EnDescanso
        {
            get { return enDescanso; }            
        }

        #endregion

        #region " Eventos Formulario "

        private void Shell_Load(object sender, EventArgs e)
        {

        }

        private void tsmiBloquear_Click(object sender, EventArgs e)
        {
            frmLogin login = new frmLogin(Program.UsuarioActivo.Login);
            login.StartPosition = FormStartPosition.CenterParent;
            login.ShowDialog();
        }

        private void tsmiListaTareas_Click(object sender, EventArgs e)
        {
            listaTareas = this.MdiChildren.FirstOrDefault(x => x.GetType() == typeof(frmListaTareas)) as frmListaTareas;
            if (listaTareas == null)
            {
                listaTareas = new frmListaTareas();
                listaTareas.MdiParent = this;
                listaTareas.Show();
                listaTareas.WindowState = FormWindowState.Maximized;
                if (todoHoy != null)
                {
                    listaTareas.OnRefrescar += todoHoy.Refrescar;
                    todoHoy.OnRefrescar += listaTareas.Refrescar;
                }
            }
            else
                listaTareas.Focus();
        }

        private void tsbListaTareas_Click(object sender, EventArgs e)
        {
            tsmiListaTareas_Click(sender, e);
        }

        private void tsmiToDoHoy_Click(object sender, EventArgs e)
        {
            todoHoy = this.MdiChildren.FirstOrDefault(x => x.GetType() == typeof(frmTodoHoy)) as frmTodoHoy;
            if (todoHoy == null)
            {
                todoHoy = new frmTodoHoy();
                todoHoy.MdiParent = this;
                todoHoy.WindowState = FormWindowState.Maximized;
                todoHoy.Show();
                if (listaTareas != null)
                {
                    todoHoy.OnRefrescar += listaTareas.Refrescar;
                    listaTareas.OnRefrescar += todoHoy.Refrescar;
                }
            }
            else
                todoHoy.Focus();
        }

        private void tsbToDoHoy_Click(object sender, EventArgs e)
        {
            tsmiToDoHoy_Click(sender, e);
        }

        private void tsmiConfiguracion_Click(object sender, EventArgs e)
        {
            frmConfiguracion configuracion = new frmConfiguracion();
            configuracion.ShowDialog();
            configuracion.Dispose();
            configuracion = null;
        }

        private void tsmiUsuarios_Click(object sender, EventArgs e)
        {
            frmUsuarios usuarios = new frmUsuarios();
            usuarios.ShowDialog();
            usuarios.Dispose();
            usuarios = null;
        }

        private void tsmiCerrarSesion_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Resources.shell_ConfirmarSesion, Resources.comun_Informacion, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                Application.Restart();
        }

        private void tsmiSalir_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Resources.shell_ConfirmarSalir, Resources.comun_Informacion, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                Application.Exit();
        }

        #endregion

        #region " Métodos "

        public void IniciarDescanso(int nroPomodoro)
        {
            if (bool.Parse(ConfigurationManager.AppSettings["ControlarDescansos"]))
            {
                if (nroPomodoro != 4)
                    display.Minutes = ConfigurationManager.AppSettings["TiempoDescansoCorto"];
                else
                    display.Minutes = ConfigurationManager.AppSettings["TiempoDescansoLargo"];

                display.Start();
                enDescanso = true;
            }
        }

        #endregion

        private void display_Stopped(object sender, EventArgs e)
        {
            string rutaWav = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            SoundPlayer player = new SoundPlayer(rutaWav + "\\TimeUp.wav");
            player.Play();
            enDescanso = false;
        }
    }
}
